package com.baosight.sczx.zm.cs.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cd.Documents;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCS;
import com.baosight.sczx.zm.cs.domain.ZMCSEngineering;

/**隶属于工程管理模块：工程信息
 * <p>Title: ServiceZMCS60.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-1-11 下午04:26:34  张小飞  Create
 *</pre>
 */
public class ServiceZMCS81 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化工程信息数据</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11下午01:11:47 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		//String parentTypes = inInfo.getString("parentTypes");//大类
		
		EiInfo outInfo = this.query(inInfo);

		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.specialFund", true));//专项资金
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.engineeringGroup", true));//工程组
		outInfo.addBlock(Department.getInstance().getConstructionUnit(true));//施工部门
		outInfo.set("engineeringNoType", SystemSettingCS.getInstance().getEngineeringNo());//工程编号形式 0自动 1手动 2生成
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.holeType", true));//钻孔类型
		/**
		if("0".equals(parentTypes)) {
			outInfo.addBlock(WorkingFace.getAllWorkingFace("cmes.cs.engineeringParentId", false));//工程所属
		}
		else if("1".equals(parentTypes)) {
			outInfo.addBlock(RoadWay.getAllRoadWay("cmes.cs.engineeringParentId", false));//工程所属
		}
		else if("2".equals(parentTypes)) {
			EiBlock _tempBlock = WorkingFace.getAllWorkingFace("cmes.cs.engineeringParentId", false);
			_tempBlock.addRows(RoadWay.getAllRoadWay("cmes.cs.engineeringParentId", false).getRows());
			outInfo.addBlock(_tempBlock);
		}
		else {
			EiBlock _tempBlock = WorkingFace.getAllWorkingFace("cmes.cs.engineeringParentId", false);
			_tempBlock.addRows(RoadWay.getAllRoadWay("cmes.cs.engineeringParentId", false).getRows());
			_tempBlock.addRows(OtherDesign.getAllOtherDesign("cmes.cs.engineeringParentId", false).getRows());
			outInfo.addBlock(_tempBlock);
		}
		*/
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11下午01:10:52 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo query(EiInfo inInfo) {
		String parentTypes = inInfo.getString("parentTypes");//大类
		String childTypes = inInfo.getString("childTypes");//小类
		
		StaticTools.setQueryParameters(inInfo, "fdType", parentTypes, true);//添加查询条件
		if(!(childTypes==null || "".equals(childTypes)))
		{
			StaticTools.setQueryParameters(inInfo, "fdClass", childTypes, true);//添加查询条件
		}

		EiInfo outInfo = super.query(inInfo, "ZMCSEngineering.query", new ZMCSEngineering());
		return outInfo;
	}
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 插入</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11下午01:10:52 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo insert(EiInfo inInfo) {
		boolean validate = this.validate(inInfo, "ZMCSEngineering.validate", new HashMap<String, String>());
		
		if(validate) {
			return inInfo;
		}
		else {
			String fdKeyId = TablePk.getInstance().getNextValue("T_CI_ENGINEERING");
			String fdDepartmentId = inInfo.getCellStr("result", 0, "fdDepartmentId");//施工部门ID
			String fdDepartment = Department.getInstance().getDepartmentNameById(fdDepartmentId);//施工部门
			
			inInfo.getBlock("result").setCell(0, "fdKeyId", fdKeyId);//主键
			inInfo.setCell("result", 0, "fdDepartment", fdDepartment);
			EiInfo outinfo = super.insert(inInfo, "ZMCSEngineering.insert");
			
			//文档 begin
			String title = inInfo.getBlock("result").getCell(0, "fdName").toString() + "设计文档";
			Documents.insertDoc(inInfo, fdKeyId, title, "", "", fdKeyId);//添加附件
			//end
			
			return outinfo;
		}
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: 更新</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11下午01:10:52 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo update(EiInfo inInfo) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validate = this.validate(inInfo, "ZMCSEngineering.validate", params);
		if(validate) {
			return inInfo;
		}
		else {
			String fdDepartmentId = inInfo.getCellStr("result", 0, "fdDepartmentId");//施工部门ID
			String fdDepartment = Department.getInstance().getDepartmentNameById(fdDepartmentId);//施工部门
			inInfo.setCell("result", 0, "fdDepartment", fdDepartment);
			
			EiInfo outInfo = super.update(inInfo, "ZMCSEngineering.update");
			
			//文档 begin
			String title = inInfo.getBlock("result").getCell(0, "fdName").toString() + "设计文档";
			Documents.insertDoc(inInfo, inInfo.getBlock("result").getCellStr(0, "fdKeyId"), title, "", "", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));//添加附件
			//end
			
			return outInfo;
		}
	}
	
	/**
	 * <p>Function: modifyStart</p>
	 * <p>Description: 开工</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-2-15下午03:54:39 张小飞
	 * </p>
	 *
	 */
	public EiInfo modifyStart(EiInfo inInfo)
	{
		String fdDepartmentId = inInfo.getCellStr("result", 0, "fdDepartmentId");//施工部门ID
		String fdDepartment = Department.getInstance().getDepartmentNameById(fdDepartmentId);//施工部门
		inInfo.setCell("result", 0, "fdDepartment", fdDepartment);
		
		EiInfo outInfo = super.update(inInfo, "ZMCSEngineering.modifyStart");
		
		return outInfo;
	}
	
	/**
	 * <p>Function: modifyStop</p>
	 * <p>Description: 竣工</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-2-15下午03:54:56 张小飞
	 * </p>
	 *
	 */
	public EiInfo modifyStop(EiInfo inInfo)
	{
		EiInfo _temp = super.update(inInfo, "ZMCSEngineering.modifyStop");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除：把删除状态修改为已删除（0）</p>
	 * <p>return value: ServiceZMCS11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-11下午01:10:52 张小飞
	 * </p>
	 *
	*/
	@Override
	public EiInfo delete(EiInfo inInfo) {
		//删除附件信息 begin
		for(int i=0; i<inInfo.getBlock("result").getRowCount(); i++)
		{
			Documents.deleteDoc(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		//end
		
		EiInfo _temp = super.delete(inInfo, "ZMCSEngineering.delete");		
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * <p>Function: validate</p>
	 * <p>Description: 校验编号、名称、简称</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-2-15下午03:58:53 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdNo = inInfo.getBlock("result").getCellStr(0, "fdNo");
		String fdName = inInfo.getBlock("result").getCellStr(0, "fdName");
		String fdAbbreviation = inInfo.getBlock("result").getCellStr(0, "fdAbbreviation");
		
		params.put("fdNo", fdNo);
		params.put("fdName", fdName);
		params.put("fdAbbreviation", fdAbbreviation);
		
		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCSEngineering _temp = (ZMCSEngineering)result.get(0);
			
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)) {
				msg.append(" 编号 ");
			}
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}
			if(_temp.getFdAbbreviation() != null && _temp.getFdAbbreviation().equals(fdAbbreviation)) {
				msg.append(" 简称 ");
			}
			msg.append("重复！");
			
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}

	/**
	 * 查询附件
	 * <p>Function: queryFiles</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30上午10:38:30 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryFiles(EiInfo inInfo) {
		String keyId = inInfo.getString("fdKeyId");
		return Documents.getFilesInfo("filesresult", keyId);
	}
}
